/*
 * Categories_Model.java
 * Final Project Example
 * Created on Oct 3, 2014, 07:58:09 PM
 * by Fairul Filza with Team FA_PemrogrammanLanjut
 */

package Models;

// <editor-fold defaultstate="collapsed" desc="Import Code">
import Database.DvdsRetail_Connection;
import Interfaces.ModelsProcess_Interface;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
// </editor-fold>

/** @author PemrogrammanLanjut_LABIV_TI_05~09 */
public class Categories_Model implements ModelsProcess_Interface {
    private String CategoryId;
    private String Description;
    private DvdsRetail_Connection Connex;
    private String[][] DataRecord;

    // <editor-fold defaultstate="collapsed" desc="Constructor Code">
    public Categories_Model() {
        Connex = new DvdsRetail_Connection();
    }

    public Categories_Model(String CategoryId, String Description) {
        Connex = new DvdsRetail_Connection();
        this.CategoryId = CategoryId;
        this.Description = Description;
    }

    public Categories_Model(String CategoryId) {
        Connex = new DvdsRetail_Connection();
        this.CategoryId = CategoryId;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Getter and Setter Code">
    public String getDescription() {
        return Description;
    }

    public void setDescription(String Description) {
        this.Description = Description;
    }

    public String getCategoryId() {
        return CategoryId;
    }

    public void setCategoryId(String CategoryId) {
        this.CategoryId = CategoryId;
    }

    public String[][] getDataRecord() {
        return DataRecord;
    }

    public void setDataRecord(String[][] DataRecord) {
        this.DataRecord = DataRecord;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Method Code">
    @Override
    public void showDataSet() {
        String Query;
        int i;

        Connex.Connection();
        try {
            Query = "SELECT CategoryId, Description FROM Categories";
            System.out.println("[QUERY] - " + Query);
            Statement Stat = Connex.getConnector().createStatement();
            ResultSet List = Stat.executeQuery(Query);
            List.last();
            DataRecord = new String[List.getRow()][2];
            List.beforeFirst();
            i = 0;
            while (List.next()) {
                DataRecord[i][0] = List.getString(1);
                DataRecord[i][1] = List.getString(2);
                i++;
            }
            List.close();
            Stat.close();
        } catch(Exception Ex) {
            System.out.println("[ERROR] - CategoriesEntity_Model.showDataSet() " + Ex.getMessage().toString());
        }
    }

    @Override
    public void showSelectedDataSet(String Value, String Key) {
        String Query;
        String Parse;

        Connex.Connection();
        try {
            Query = "SELECT CategoryId, Description FROM Categories ";
            if(Key.equals("CategoryId")) {
                Parse = "WHERE CategoryId = '" + Value + "'";
            } else if(Key.equals("Description")) {
                Parse = "WHERE Description = '" + Value + "'";
            } else {
                Parse = "";
            }
            Query = Query + Parse;
            System.out.println("[QUERY] - " + Query);
            Statement Stat = Connex.getConnector().createStatement();
            ResultSet List = Stat.executeQuery(Query);
            while (List.next()) {
                CategoryId = List.getString(1);
                Description = List.getString(2);
            }
            List.close();
            Stat.close();
        } catch(Exception Ex) {
            System.out.println("[ERROR] - Categories_Model.showSelectedDataSet() " + Ex.getMessage().toString());
        }
    }

    @Override
    public boolean addDataValue() {
        String Query;

        Connex.Connection();
        try {
            Query = "INSERT INTO Categories VALUES('"+ CategoryId +"','"+ Description +"')";
            System.out.println("[QUERY] - " + Query);
            Statement Stat = Connex.getConnector().createStatement();
            Stat.executeUpdate(Query);
            Stat.close();
            return true;
        } catch (SQLException Ex) {
            System.out.println("[ERROR] - Categories_Model.addDataValue() " + Ex.getMessage().toString());
            return false;
        }

    }
    @Override
    public boolean editDataValue() {
        String Query;

        Connex.Connection();
        try {
            Query = "UPDATE Categories SET Description = '" + Description + "' "
                    + "WHERE CategoryId = '" + CategoryId + "'";
            System.out.println("[QUERY] - " + Query);
            Statement Stat = Connex.getConnector().createStatement();
            Stat.executeUpdate(Query);
            Stat.close();
            return true;
        } catch (SQLException Ex) {
            System.out.println("[ERROR] - Categories_Model.editDataValue() " + Ex.getMessage().toString());
            return false;
        }
    }

    @Override
    public boolean subDataValue() {
        String Query;

        Connex.Connection();
        try {
            Query = "DELETE FROM Categories WHERE CategoryId = '" + CategoryId + "'";
            System.out.println("[QUERY] - " + Query);
            Statement Stat = Connex.getConnector().createStatement();
            Stat.executeUpdate(Query);
            Stat.close();
            return true;
        } catch (SQLException Ex) {
            System.out.println("[ERROR] - Categories_Model.subDataValue() " + Ex.getMessage().toString());
            return false;
        }
    }

    @Override
    public String genIdDataValue() {
        String Query;
        String Result;
        int hasil;

        Connex.Connection();
        try {
            Result = null;
            Query = "SELECT CategoryId FROM Categories ORDER BY CategoryId DESC LIMIT 1";
            System.out.println("[QUERY] - " + Query);
            Statement Stat = Connex.getConnector().createStatement();
            ResultSet List = Stat.executeQuery(Query);
            while (List.next()) {
                Result = List.getString(1).trim();
            }
            List.close();
            Stat.close();

            if (!Result.equals("")) {
                hasil = Integer.parseInt(Result) + 1;
                Result = String.valueOf(hasil);
                if (Result.length() == 1) {
                    Result = "0" + Result;
                } 
            } else {
                Result = "01";
            }
            return Result;
        } catch (Throwable Ex) {
            System.out.println("[ERROR] - Categories_Model.genIdDataValue() " + Ex.getMessage());
            return "01";
        }
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Tester Code">
//    public void methodTester(int flag) {
//        switch(flag) {
//            case 0:
//                showDataSet();
//                System.out.println("Result = " + this.DataRecord[1][1]);
//                break;
//            case 1:
//                showSelectedDataSet("0001", "CategoryId");
//                System.out.println("Result = " + this.Description);
//                break;
//            case 2:
//                System.out.println("Result = " + addDataValue());
//                break;
//            case 3:
//                System.out.println("Result = " + editDataValue());
//                break;
//            case 4:
//                System.out.println("Result = " + subDataValue());
//                break;
//            case 5:
//                System.out.println("Result = " + genIdDataValue());
//                break;
//        }
//    }
//
//    public static void main(String[] args) {
//        Categories_Model Me = new Categories_Model("03", "Horor");
////        Categories_Model Me = new Categories_Model();
//        Me.methodTester(2);
//    }
    // </editor-fold>
}
// End of Code